package com.sitanyun.merchant.guide.frament.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.adapter.RankAdapter;
import com.sitanyun.merchant.guide.adapter.RankBaseAdapter;
import com.sitanyun.merchant.guide.adapter.RecyleViewDialsAdapter;
import com.sitanyun.merchant.guide.base.BaseFragment;
import com.sitanyun.merchant.guide.bean.AnatotalBean;
import com.sitanyun.merchant.guide.bean.RankBean;
import com.sitanyun.merchant.guide.bean.Scenlistsbean;
import com.sitanyun.merchant.guide.bean.ScenslistdialsBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.frament.view.activity.AnalysDiailsActivity;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.ListScrollView;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * A simple {@link Fragment} subclass.
 * 经营分析
 */
public class AnalysisFragment extends BaseFragment {

    @BindView(R.id.tv_return)
    ImageView tvReturn;
    @BindView(R.id.tv_title_str)
    TextView tvTitleStr;
    @BindView(R.id.image_right_key)
    ImageView imageRightKey;
    @BindView(R.id.tv_right_key)
    TextView tvRightKey;
    @BindView(R.id.ff_main_layout)
    FrameLayout ffMainLayout;
    @BindView(R.id.tv_dails)
    TextView tvDails;
    @BindView(R.id.tv_yesterday)
    TextView tvYesterday;
    @BindView(R.id.shar_dails)
    LinearLayout sharDails;
    @BindView(R.id.tv_tp)
    TextView tvTp;
    @BindView(R.id.tv_cj)
    TextView tvCj;
    @BindView(R.id.tv_browse)
    TextView tvBrowse;
    @BindView(R.id.tv_share)
    TextView tvShare;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_metting)
    TextView tvMetting;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_call)
    TextView tvCall;
    @BindView(R.id.rv_rank)
    RecyclerView rvRank;
    @BindView(R.id.scen_fh)
    ImageView scenFh;
    @BindView(R.id.shar)
    TextView shar;
    @BindView(R.id.browse)
    TextView browse;
    @BindView(R.id.call)
    TextView call;
    @BindView(R.id.meeting)
    TextView meeting;
    @BindView(R.id.scen_fhs)
    ImageView scenFhs;
    @BindView(R.id.lv_rank)
    ListView lvRank;
    @BindView(R.id.list_scroll)
    ListScrollView listScroll;
    private PopupWindow popupWindow;
    private String type;
    private String queryType;
    private RankAdapter rankAdapter;
    private Scenlistsbean scenlistsbean;
    private String scenid;
    private RankBaseAdapter rankBaseAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_analysis;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        tvReturn.setVisibility(View.GONE);
        tvTitleStr.setText("经营分析");

    }

    @Override
    public void onResume() {
        super.onResume();
        tvYesterday.setText("昨天");
        tvTp.setText("同屏讲解时长");
        //查询数据总览   //昨天，近一周，近一个月，近一年
        //(年:3月:2周:1) 0zuotian
        type = "0";
        //数据总览接口
        inittotaldata(type);
        //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
        //返回数据
        //创建布局管理
        queryType = "5";
        //排行接口
        initstaffrank(queryType, type);
        //查询场景列表
        initscen(type);
    }

    @Override
    public void initView() {


    }

    //场景
    private void initscen(String type) {
        OkHttpUtils.get().url(Urls.scencelist)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                scenlistsbean = new Gson().fromJson(response, Scenlistsbean.class);
                if (scenlistsbean.getCode() == 0) {
                    //场景数据接口
                    if (scenlistsbean.getData().size() == 0) {
                        tvCj.setVisibility(View.GONE);
                        scenFhs.setVisibility(View.GONE);
                        return;
                    } else {
                        tvCj.setVisibility(View.VISIBLE);
                        scenFhs.setVisibility(View.VISIBLE);
                        scenid = scenlistsbean.getData().get(0).getSceneId();
                        if (scenlistsbean.getData().get(0).getSceneName() == null) {
                            tvCj.setText("  ");
                        } else {
                            tvCj.setText(scenlistsbean.getData().get(0).getSceneName() + "");
                        }

                        initscencedata(scenlistsbean.getData().get(0).getSceneId(), type);
                    }

                }
            }
        });
    }

    //场景数据接口
    private void initscencedata(String scenid, String type) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("nodeId", SharedPreferenceUtil.getStringData("tonodeid"));
        hashMap.put("scenceId", scenid);
        hashMap.put("type", type);
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .url(Urls.scencedata)
                .content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                ScenslistdialsBean scenslistdialsBean = new Gson().fromJson(response, ScenslistdialsBean.class);
                if (scenslistdialsBean.getCode() == 0) {
                    shar.setText(scenslistdialsBean.getData().getShare() + "次");
                    browse.setText(scenslistdialsBean.getData().getBrowse() + "次");
                    call.setText(scenslistdialsBean.getData().getCalls() + "次");
                    meeting.setText(scenslistdialsBean.getData().getInterpret() + "次");

                }
            }
        });

    }

    //员工排行榜
    private void initstaffrank(String queryType, String type) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("nodeId", SharedPreferenceUtil.getStringData("tonodeid"));
        hashMap.put("queryType", queryType);
        hashMap.put("type", type);
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .url(Urls.staffrank)
                .content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                RankBean rankBean = new Gson().fromJson(response, RankBean.class);
                if (rankBean.getCode() == 0) {
                    rankBaseAdapter = new RankBaseAdapter(getActivity(), rankBean.getData());
//                    if (rankBean.getData().size()==0){
//                        return;
//                    }
                    listScroll.setListView(lvRank);
                    //   setListViewHeightBaseOnChildren(lvRank,10);
                    View listItem = rankBaseAdapter.getView(0, null, lvRank);
                    listItem.measure(0, 0);
                    int totalHei ;
                    if (rankBaseAdapter.getCount()>10){
                        totalHei = (listItem.getMeasuredHeight() + lvRank.getDividerHeight()) * 10;
                    }else {
                        totalHei = (listItem.getMeasuredHeight() + lvRank.getDividerHeight()) * rankBaseAdapter.getCount();
                    }

                    lvRank.getLayoutParams().height = totalHei;
                    lvRank.setAdapter(rankBaseAdapter);
                    rankBaseAdapter.notifyDataSetChanged();
//                    rankAdapter.setNewData(rankBean.getData());
//                    rankAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    /**
     * 获取并设置listview 的高度
     *
     * @param listView
     * @param maxCount 设置最大可显示条目数量
     */
    public void setListViewHeightBaseOnChildren(ListView listView, int maxCount) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        if (listAdapter.getCount() > maxCount) {
            for (int i = 0; i < maxCount; i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            params.height = totalHeight + (listView.getDividerHeight() * (maxCount - 1));
        } else {
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        }

        listView.setLayoutParams(params);
    }

    private void inittotaldata(String type) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("nodeId", SharedPreferenceUtil.getStringData("tonodeid"));
        hashMap.put("type", type);
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .url(Urls.totaldata)
                .content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                AnatotalBean anatotalBean = new Gson().fromJson(response, AnatotalBean.class);
                if (anatotalBean.getCode() == 0) {
                    tvBrowse.setText(anatotalBean.getData().getBrowseCust() + "人");
                    tvShare.setText(anatotalBean.getData().getShare() + "次");
                    tvPhone.setText(anatotalBean.getData().getPhoneCust() + "人");
                    tvMetting.setText(anatotalBean.getData().getInterpretCnt() + "次");
                    tvMessage.setText(anatotalBean.getData().getMessageCust() + "次");
                    tvCall.setText(anatotalBean.getData().getCallCnt() + "次");
                }
            }
        });
    }

    //**详情
    @OnClick(R.id.tv_dails)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), AnalysDiailsActivity.class));
    }

    @OnClick({R.id.tv_yesterday, R.id.shar_dails, R.id.tv_tp, R.id.tv_cj})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //选择时间
            case R.id.tv_yesterday:
                initpopu(view);
                break;
            case R.id.shar_dails:
                break;
            //排行榜筛选
            case R.id.tv_tp:
                scenFh.animate().setDuration(500).rotation(180).start();
                initpopuwindows(view);
                break;
            case R.id.tv_cj:
                scenFhs.animate().setDuration(500).rotation(180).start();
                initpopuwindow(view);
                break;
        }
    }

    //场景数据下啦
    private void initpopuwindow(View view) {
        View contentView = LayoutInflater.from(getActivity()).inflate(
                R.layout.popu_listcj, null);
        RecyclerView rvlist = contentView.findViewById(R.id.scen_list_rv);
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvlist.setLayoutManager(layoutManager);
        RecyleViewDialsAdapter recyleViewDialsAdapter = new RecyleViewDialsAdapter(getActivity());
        rvlist.setAdapter(recyleViewDialsAdapter);
        recyleViewDialsAdapter.notifyDataSetChanged();
        recyleViewDialsAdapter.setNewData(scenlistsbean.getData());
        recyleViewDialsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.list:
                        popupWindow.dismiss();
                        scenid = scenlistsbean.getData().get(position).getSceneId();
                        initscencedata(scenlistsbean.getData().get(position).getSceneId(), type);
                        if (scenlistsbean.getData().get(position).getSceneName() == null) {
                            tvCj.setText("  ");
                        } else {
                            tvCj.setText(scenlistsbean.getData().get(position).getSceneName() + "");
                        }

                        //   tvCj.setText(scenlistsbean.getData().get(position).getSceneName()+"");
                        // ToastUtil.showToast(getActivity(),"123");
                        break;
                }
            }
        });
        popupWindow = new PopupWindow(contentView,
                330, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //   Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                scenFhs.animate().setDuration(500).rotation(0).start();
            }
        });
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.baise));

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);

        recyleViewDialsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.e("TAGS", scenlistsbean.getData().get(position).getSceneId());
                popupWindow.dismiss();
            }
        });
    }

    // 筛选
    private void initpopuwindows(View view) {
        View contentView = LayoutInflater.from(getActivity()).inflate(
                R.layout.popu_scen, null);
        Button share = (Button) contentView.findViewById(R.id.share);
        Button browse = (Button) contentView.findViewById(R.id.browse);
        Button metting = (Button) contentView.findViewById(R.id.metting);
        Button time = (Button) contentView.findViewById(R.id.time);
        Button mesting = (Button) contentView.findViewById(R.id.mesting);
        Button mestingnum = (Button) contentView.findViewById(R.id.mestingnum);
        //分享数量  mestingnum
        share.setText("分享次数");
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTp.setText("分享次数");
                if (tvYesterday.getText().toString().equals("昨天")) {
                    type = "0";
                } else if (tvYesterday.getText().toString().equals("近一周")) {
                    type = "1";
                } else if (tvYesterday.getText().toString().equals("近一个月")) {
                    type = "2";
                } else if (tvYesterday.getText().toString().equals("近一年")) {
                    type = "3";
                }
                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                initstaffrank("1", type);

                ///    Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                rankBaseAdapter.notifyDataSetChanged();

            }
        });
        //浏览数量
        browse.setText("被浏览次数");
        browse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTp.setText("被浏览次数");
                ///     Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                if (tvYesterday.getText().toString().equals("昨天")) {
                    type = "0";
                } else if (tvYesterday.getText().toString().equals("近一周")) {
                    type = "1";
                } else if (tvYesterday.getText().toString().equals("近一个月")) {
                    type = "2";
                } else if (tvYesterday.getText().toString().equals("近一年")) {
                    type = "3";
                }
                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                initstaffrank("2", type);
                rankBaseAdapter.notifyDataSetChanged();
            }
        });
        //同屏讲解次数
        metting.setText("互动留言次数");
        metting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTp.setText("互动留言次数");
                ///      Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                if (tvYesterday.getText().toString().equals("昨天")) {
                    type = "0";
                } else if (tvYesterday.getText().toString().equals("近一周")) {
                    type = "1";
                } else if (tvYesterday.getText().toString().equals("近一个月")) {
                    type = "2";
                } else if (tvYesterday.getText().toString().equals("近一年")) {
                    type = "3";
                }
                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                initstaffrank("3", type);
                rankBaseAdapter.notifyDataSetChanged();
            }
        });
        //上传时间
        time.setText("要求回电次数");
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTp.setText("要求回电次数");
                ///       Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                if (tvYesterday.getText().toString().equals("昨天")) {
                    type = "0";
                } else if (tvYesterday.getText().toString().equals("近一周")) {
                    type = "1";
                } else if (tvYesterday.getText().toString().equals("近一个月")) {
                    type = "2";
                } else if (tvYesterday.getText().toString().equals("近一年")) {
                    type = "3";
                }
                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                initstaffrank("4", type);
                rankBaseAdapter.notifyDataSetChanged();
            }
        });
        mesting.setVisibility(View.VISIBLE);
        mesting.setText("同屏讲解时长");
        mesting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTp.setText("同屏讲解时长");
                popupWindow.dismiss();
                if (tvYesterday.getText().toString().equals("昨天")) {
                    type = "0";
                } else if (tvYesterday.getText().toString().equals("近一周")) {
                    type = "1";
                } else if (tvYesterday.getText().toString().equals("近一个月")) {
                    type = "2";
                } else if (tvYesterday.getText().toString().equals("近一年")) {
                    type = "3";
                }
                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                initstaffrank("5", type);
                rankBaseAdapter.notifyDataSetChanged();
            }
        });
        mestingnum.setVisibility(View.VISIBLE);
        mestingnum.setText("同屏讲解次数");
        mestingnum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTp.setText("同屏讲解次数");
                popupWindow.dismiss();
                if (tvYesterday.getText().toString().equals("昨天")) {
                    type = "0";
                } else if (tvYesterday.getText().toString().equals("近一周")) {
                    type = "1";
                } else if (tvYesterday.getText().toString().equals("近一个月")) {
                    type = "2";
                } else if (tvYesterday.getText().toString().equals("近一年")) {
                    type = "3";
                }
                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                initstaffrank("6", type);
                rankBaseAdapter.notifyDataSetChanged();
            }
        });
        popupWindow = new PopupWindow(contentView,
                330, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //   Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                scenFh.animate().setDuration(500).rotation(0).start();
            }
        });
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.baise));

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
        //      popupWindow.showAtLocation(tvScreen, Gravity.BOTTOM,0,0);
        //  Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
    }

    //日期选择
    private void initpopu(View view) {
        View contentView = LayoutInflater.from(getActivity()).inflate(
                R.layout.popu_scen, null);
        Button share = (Button) contentView.findViewById(R.id.share);
        Button browse = (Button) contentView.findViewById(R.id.browse);
        Button metting = (Button) contentView.findViewById(R.id.metting);
        Button time = (Button) contentView.findViewById(R.id.time);
        //分享数量
        share.setText("昨天");
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvYesterday.setText("昨天");
                ///    Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                //查询数据总览   //昨天，近一周，近一个月，近一年
                //(年:3月:2周:1) 0zuotian
                type = "0";
                //数据总览接口
                inittotaldata(type);
                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                //返回数据
                //创建布局管理
                if (tvTp.getText().equals("分享次数")) {
                    queryType = "1";
                } else if (tvTp.getText().equals("被浏览次数")) {
                    queryType = "2";
                } else if (tvTp.getText().equals("互动留言次数")) {
                    queryType = "3";
                } else if (tvTp.getText().equals("要求回电次数")) {
                    queryType = "4";
                } else if (tvTp.getText().equals("同屏讲解时长")) {
                    queryType = "5";
                }

                rankBaseAdapter.notifyDataSetChanged();
                //排行接口
                initstaffrank(queryType, type);
                //查询场景列表
                initscencedata(scenid, type);
            }
        });
        //浏览数量
        browse.setText("近一周");
        browse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvYesterday.setText("近一周");
                ///     Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();

                //查询数据总览   //昨天，近一周，近一个月，近一年
                //(年:3月:2周:1) 0zuotian
                type = "1";
                //数据总览接口
                inittotaldata(type);

                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                //返回数据
                //创建布局管理
                if (tvTp.getText().equals("分享次数")) {
                    queryType = "1";
                } else if (tvTp.getText().equals("被浏览次数")) {
                    queryType = "2";
                } else if (tvTp.getText().equals("互动留言次数")) {
                    queryType = "3";
                } else if (tvTp.getText().equals("要求回电次数")) {
                    queryType = "4";
                } else if (tvTp.getText().equals("同屏讲解时长")) {
                    queryType = "5";
                }

                rankBaseAdapter.notifyDataSetChanged();
                //排行接口
                initstaffrank(queryType, type);
                //查询场景列表
                initscencedata(scenid, type);
            }
        });
        //同屏讲解次数
        metting.setText("近一个月");
        metting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvYesterday.setText("近一个月");
                ///      Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                //查询数据总览   //昨天，近一周，近一个月，近一年
                //(年:3月:2周:1) 0zuotian
                type = "2";
                //数据总览接口
                inittotaldata(type);
                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                //返回数据
                //创建布局管理
                if (tvTp.getText().equals("分享次数")) {
                    queryType = "1";
                } else if (tvTp.getText().equals("被浏览次数")) {
                    queryType = "2";
                } else if (tvTp.getText().equals("互动留言次数")) {
                    queryType = "3";
                } else if (tvTp.getText().equals("要求回电次数")) {
                    queryType = "4";
                } else if (tvTp.getText().equals("同屏讲解时长")) {
                    queryType = "5";
                }

                rankBaseAdapter.notifyDataSetChanged();
                //排行接口
                initstaffrank(queryType, type);
                //查询场景列表
                initscencedata(scenid, type);
            }
        });
        //上传时间
        time.setText("近一年");
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvYesterday.setText("近一年");
                ///       Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
                popupWindow.dismiss();
                //查询数据总览   //昨天，近一周，近一个月，近一年
                //(年:3月:2周:1) 0zuotian
                type = "3";
                //数据总览接口
                inittotaldata(type);
                //员工排行榜 分享次数1,被浏览次数2,互动留言次数3,要求回电次数4,同屏讲解时长5
                //返回数据
                //创建布局管理
                if (tvTp.getText().equals("分享次数")) {
                    queryType = "1";
                } else if (tvTp.getText().equals("被浏览次数")) {
                    queryType = "2";
                } else if (tvTp.getText().equals("互动留言次数")) {
                    queryType = "3";
                } else if (tvTp.getText().equals("要求回电次数")) {
                    queryType = "4";
                } else if (tvTp.getText().equals("同屏讲解时长")) {
                    queryType = "5";
                }

                rankBaseAdapter.notifyDataSetChanged();
                //排行接口
                initstaffrank(queryType, type);
                //查询场景列表
                initscencedata(scenid, type);
            }
        });
        popupWindow = new PopupWindow(contentView,
                330, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //   Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

            }
        });
//        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                scenFh.animate().setDuration(500).rotation(0).start();
//            }
//        });
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.baise));

        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
        //      popupWindow.showAtLocation(tvScreen, Gravity.BOTTOM,0,0);
        //  Glide.with(getActivity()).load(R.mipmap.down_item).into(scenFh);
    }
}
